<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas" width="500" height="600" style="border: 1px solid grey"></canvas>
    <input type="button" value="清空Canvas">
</body>
</html>
<script>

    var cns = document.querySelector('#canvas');
    var context = cns.getContext("2d");
    console.log(cns.width,cns.height);

    // 描边矩形
    context.strokeStyle='red';
    context.lineWidth=3;
    context.strokeRect(50, 50, 100, 80);

    // 填充矩形
    context.fillStyle='pink';
    context.fillRect(150, 150, 100, 80);

    // 描边与填充结合
    context.strokeStyle = "green";
    context.strokeRect(200, 200, 150, 150);
    context.fillStyle = "blue";
    context.fillRect(200, 200, 150, 150);

    var btn = document.querySelector('input');
    
    btn.addEventListener('click',function(){

        context.clearRect(0, 0, cns.width, cns.height);
        
    });
    
    
    






</script>